﻿CREATE PROCEDURE [dbo].[proc_Order_FinalPayment_Getlist]
	(
		@CompanyId int,
		@SWId int,
		@Stext nvarchar(50),
		@StartDate nvarchar(10),
		@EndDate nvarchar(10),
		@StartIndex int,
		@EndIndex int
	)
AS
Begin
	Declare @sRq Datetime,@eRq Datetime
	Set @sRq = Convert(Datetime,@StartDate+' 00:00:00')
	Set @eRq = Convert(Datetime,@EndDate+' 23:59:59')

	;WITH list As(Select ROW_NUMBER() OVER (ORDER BY op.FinanceFlag)AS Row,
		op.Id
		,op.GId
		,op.OId
		,op.CusId
		,op.CusName
		,op.SalesmanAccount
		,op.Salesman
		,op.CreateDate
		,op.Prices
		,op.PaymentTypeId
		,op.PaymentType
		,op.BankId
		,op.Bank
		,op.SWId
		,op.SWName
		,op.shjbh
		,op.FinanceFlag
		,o.AuditDate OrderAuditDate
		,ISnull(op.FinanceDate,'1900-01-01') As FinanceDate
		,op.CompanyId
		,op.[Description]
		,IsNull(o.NOE_Flag,0) As NOE_Flag
		From Order_PaymentRecord op join [Order] o On op.OId=o.OId
		Where (@CompanyId=0 Or op.CompanyId=@CompanyId) And (@SWId=0 Or op.SWId=@SWId) 
			And op.CreateDate>=@sRq And op.CreateDate<=@eRq 
			And (@Stext is null Or op.CusName like '%'+@Stext+'%' Or op.Salesman like '%'+@Stext+'%') 
	)
	
	Select *,(Select Count(Id) From list) AS cid 
	From list Where Row Between @StartIndex and @EndIndex Order By Row
End
